How do I recursively call a prototype function inside itself while looping an array in JavaScript